package com.salton123.soulove.sailfish.video.ui.aty

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.salton123.soulove.api.RouterManager
import com.salton123.soulove.app.R
import com.salton123.soulove.common.Constants
import com.salton123.soulove.sailfish.db.SailfishDatabase
import com.salton123.soulove.sailfish.video.SailfishManager
import com.salton123.soulove.sailfish.video.ui.adapter.VideoSearchAdapter
import com.salton123.ui.base.BaseActivity
import kotlinx.android.synthetic.main.activity_search_video.*
import kotlinx.android.synthetic.main.default_search_title.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * Time:2021/9/6 9:18 下午
 * Author:
 * Description:
 */
@Deprecated("废弃不用了")
@Route(path = Constants.Router.Video.SEARCH)
class VideoSearchActivity : BaseActivity() {
    var dirName = "*"
    override fun getLayoutId(): Int = R.layout.activity_search_video
    private val mAdapter by lazy { VideoSearchAdapter() }
    override fun enableTitleBar(): Boolean = false
    override fun initViewAndData(savedInstanceState: Bundle?) {
        recyclerView.adapter = mAdapter
        recyclerView.layoutManager = LinearLayoutManager(activity())
        inflateTitleBar(true, R.layout.default_search_title) {
            etInput.addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
                }

                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                }

                override fun afterTextChanged(s: Editable?) {
                    lifecycleScope.launch {
                        val ret = SailfishDatabase.Impl.videoDao().fluzzySearch(dirName, s.toString())
                        withContext(Dispatchers.Main) {
                            mAdapter.setNewInstance(ret)
                        }
                    }
                }
            })
            tvTitleSearch.setOnClickListener {
                finish()
            }
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->
            SailfishManager.toPlayVideoList = mAdapter.data
            RouterManager.videoPlay(activity(), position)
        }
        lifecycleScope.launch {
            val ret = SailfishDatabase.Impl.videoDao().fluzzySearch(dirName, "")
            withContext(Dispatchers.Main) {
                mAdapter.setNewInstance(ret)
            }
        }
    }
}